package com.lz.controller;

import com.lz.convertor.OrderForm2OrderMasterQueryConvertor;
import com.lz.entity.OrderMasterQuery;
import com.lz.enums.ResultEnum;
import com.lz.exception.SellException;
import com.lz.form.OrderForm;
import com.lz.service.BuyerService;
import com.lz.service.OrderService;
import com.lz.utils.ResultVOUtil;
import com.lz.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户订单
 * Created by lz on 2018/8/22.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BuyerService buyerService;

    /**
     * 创建订单
     */
    @PostMapping("/create")
    public ResultVO<Map<String,String>> create(@Valid OrderForm orderForm,
                                               BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("【创建订单】参数不正确，orderForm={}",orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        OrderMasterQuery convert = OrderForm2OrderMasterQueryConvertor.convert(orderForm);
        if (CollectionUtils.isEmpty(convert.getOrderDetailList())){
            log.error("【创建订单】购物车不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }

        OrderMasterQuery orderMasterQuery = orderService.createOrder(convert);

        Map<String,String> map = new HashMap<>();
        map.put("orderId",orderMasterQuery.getOrderId());

        return ResultVOUtil.success(map);
    }

    /**
     * 订单列表
     */
    @GetMapping("/list")
    public ResultVO<List<OrderMasterQuery>> list(@RequestParam("openid") String openid,
                                                 @RequestParam(value = "page",defaultValue = "0") Integer page,
                                                 @RequestParam(value = "size",defaultValue = "10") Integer size){
        if (StringUtils.isEmpty(openid)){
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        Pageable pageable = new PageRequest(page,size);
        Page<OrderMasterQuery> list = orderService.findList(openid, pageable);
        return ResultVOUtil.success(list.getContent());
    }

    /**
     * 订单详情
     */
    @GetMapping("/detail")
    public ResultVO<OrderMasterQuery> detail(@RequestParam("openid") String openid,
                                             @RequestParam("orderId") String orderId){
        //检查订单和用户匹配
        OrderMasterQuery orderOne = buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(orderOne);
    }

    /**
     * 订单取消
     */
    @PostMapping("cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
        //取消订单是检查匹配
        buyerService.cancelOrder(openid, orderId);

        return ResultVOUtil.success();
    }
}
